import { API_CONFIG } from '../../../config'
Page({
  data: {
    bgStyle: `background-image: url('${API_CONFIG.staticUrl}/wx-x-mai/questionnaireBg.jpg')`,
    showForm: '', // 下滑样式
    bIsShowContent: true, // 隐藏内容
    bIsShowIcon: false, // 显示向下箭头
    questionnaireData: [], // 获取问卷调查题目列表data
    questionnaireTitle: '', // 小程序介绍内容
    formTitle: '', // 小程序问卷调查名称
    appMsg: {
      type: '', // 状态
      show: false // 是否展示
    },
    val: "",
    issueTypeList: [
      {
        id: 1,
        name: "单选"
      },
      {
        id: 2,
        name: "多选"
      },
      {
        id: 3,
        name: "问答"
      },
    ],

    hidden: true,
    nocancel: false,
    questionnaireId: "",
    schoolCode: "",
    userId: ""
  },
  onLoad (options) {
    this.data.questionnaireId = options.questionnaireId;
    this.data.schoolCode = options.schoolCode;
    this.data.userId = options.userId;
    this.getDetails();
    // this.getQuestionnaireList();
  },


  getDetails () {
    const request = {
      schoolCode: this.data.schoolCode,
      studentId: this.data.userId,
      questionnaireId: this.data.questionnaireId
    }
    wx.$API['questionnaire/getDetails'](request).then((response) => {

      const { title2, questionnaireSubjects } = response.data.quIssueList;

      response.data.quIssueList.forEach(itemData => {
        console.log(itemData, "拿到每一项")
        if (itemData.quIssueOptionList) {
          itemData.quIssueOptionList.forEach(v => {
            v.isOptiontitle = false;
          });
          console.log(itemData.quIssueOptionList, "")
        }
        if (itemData.issueType === 2 || itemData.issueType === 3) {
          itemData.checkValueText = itemData.quIssueOptionList;
        }
        itemData.value = ''
      })
      this.setData({
        questionnaireData: response.data.quIssueList,
        questionnaireTitle: response.data.questionnaireTitle,
        formTitle: response.data.questionnaireTitle
      })




    }).catch((error) => {
      console.log(error)
      wx.showToast({
        title: error,
        icon: "none"
      })
    })
  },
  handleShow (event) {
    const { scrollTop } = event.detail
    if (scrollTop < 250 && this.data.showForm !== '') {
      this.setData({
        showForm: '',
        bIsShowContent: true,
        bIsShowIcon: false
      })
      return
    }

    if (scrollTop > 250 && this.data.showForm !== 'show-form') {
      this.setData({
        showForm: 'show-form',
        bIsShowContent: false,
        bIsShowIcon: true
      })
    }
  },


  cancel: function () {
    this.setData({
      hidden: true
    });
  },
  confirm: function () {
    this.setData({
      hidden: true
    });
    this.submitQu();
  },
  /**
   * @see 接口-获取问卷调查题目列表
   */
  getQuestionnaireList () {
    wx.$API['questionnaire/getQuestionnaireList']({}).then((response) => {
      const { title1, title2, questionnaireSubjects } = response.data
      questionnaireSubjects.forEach(itemData => {
        if (itemData.questionType === 2 || itemData.questionType === 3) {
          itemData.checkValueText = itemData.checkValue.split(',') // 返回来的单选、多选的选项是字符串格式，转化为数组
        }
        itemData.value = ''
      })
      this.setData({
        questionnaireData: questionnaireSubjects,
        questionnaireTitle: title1,
        formTitle: title2
      })
    }).catch((error) => {
      wx.showToast({
        title: error,
        icon: 'none',
        duration: 5000
      })
    })
  },
  /**
   * @see 接口-提交问卷调查答案
   */
  submitQu () {
    const questionnaireAnswers = [];
    console.log(this.data.questionnaireData, "拿到答题")
    let objarray = [];
    let optionId;
    this.data.questionnaireData.forEach(item => {
      if (item.quIssueOptionList) {
        item.quIssueOptionList.forEach(v => {
          if (item.value == v.optionContent) {
            optionId = v.optionId;
          }
        });
        item.optionId = optionId;
      }
      item.content = item.value;
    })

    // const request = {
    //   quAnswerList: this.data.questionnaireData,
    //   schoolCode: this.data.schoolCode,
    //   userId: this.data.userId,
    //   questionnaireId: this.data.questionnaireId
    // }
    // var requests = request.request
    // console.log(request, "拿到提交参数")

    wx.$API['questionnaire/submitQu'](
      {
        quAnswerList: this.data.questionnaireData,
        schoolCode: this.data.schoolCode,
        userId: this.data.userId,
        questionnaireId: this.data.questionnaireId
      }
    ).then((response) => {
      wx.showToast({
        title: response,
        duration: 2000
      })
      setTimeout(() => {
        wx.switchTab({
          url: '../../home/index',
        })
      }, 3000);
    }).catch((error) => {
      wx.showToast({
        title: error,
        icon: 'none',
        duration: 2000
      })

      setTimeout(() => {
        wx.switchTab({
          url: '../../home/index',
        })
      }, 3000);
    })

  },
  /**
   * @see 多选
   */
  checkboxChange (e) {
    const index = e.currentTarget.dataset.index
    const value = e.detail.value
    this.setData({
      [`questionnaireData[${index}].value`]: value.join()
    })
  },
  /**
   * @see 单选
   */
  radioChange (e) {
    console.log(e, "拿到选中")
    const index = e.currentTarget.dataset.index
    const value = e.detail.value;
    if (value == e.target.dataset.item.value) {
      this.setData({
        val: e.detail.value
      })
    }
    this.setData({
      [`questionnaireData[${index}].value`]: value
    })
  },
  /**
   * 
   * @see 简答题
   */
  inputChange (e) {
    const index = e.currentTarget.dataset.index
    const value = e.detail.value
    this.setData({
      [`questionnaireData[${index}].value`]: value
    })
  },
  /**
   * @see 点击提交按钮
   */
  handleSubmit () {
    this.setData({
      hidden: false
    });
  }
})
